<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Front extends La_Controller {

	public function __construct() {
		parent::__construct();
		$this->load->model('Category_model');
		$this->load->model('City_model');
		$this->load->model('User_model');
		$this->data['first_level_categories'] = $this->Category_model->find_son_category(1);

		
		
		$this->data['provinces'] = $this->City_model->find_all_province();
		$this->data['nav'] = 'expert';
	}

	public function index()
	{
		$current_category_id = $this->uri->segment(2);
		if (empty($current_category_id)) {
			$current_category_id = 2;
		}
		$this->data['current_category_id'] = $current_category_id;
		$this->data['categories'] = $this->Category_model->find_all_child_category($current_category_id);
		$this->load->view('front/index', $this->data);
	
	}
	
	public function find_sub_categories_ajax() {
		$section_id = intval($this->input->get('section_id'));
		$this->load->model('Category_model');
		$sub_categories = $this->Category_model->find_son_category($section_id);

		echo json_encode($sub_categories);
		exit;
	}
	public function test() {

//		$this->load->model('Category_model');
//		
//		$this->Category_model->insert_child_category("根分类", 0);
//		echo $this->Category_model->insert_child_category("生活服务", 1);
//		$this->Category_model->insert_child_category("商业服务", 1);
//		$this->Category_model->insert_child_category("文化文艺", 1);
//		$this->Category_model->insert_child_category("体育运动", 1);
//		$this->Category_model->insert_child_category("服务行业", 1);
//		$this->Category_model->insert_child_category("制造行业", 1);
//		$this->Category_model->insert_child_category("农林牧渔", 1);
//		for($i=1; $i<10; $i++) {
//			$this->Category_model->insert_child_category("生活二级" . $i, 2);
//		}
		//for($i=10; $i<18; $i++) {
//			for($j=1;$j<10; $j++) {
//				$this->Category_model->insert_child_category("生活三级9-" . $j, 18);
//			}
		//}
		//$this->Category_model->delete_category_with_child(10);
		//print_r($this->Category_model->find_grandson_category(2));
		//die("ok");
		
		//	$this->load->library('utility');
	//	$this->load->library('pagination');
	//	print_r($this->City_model->find_all_cities(110000));
		
		//print $this->User_model->is_user_exist("admin@localhost.com");
		//print_r($this->User_model->find_user_by_email("admin@localhost.com"));
		
		//echo $this->utility->captcha();
		

//		$config['base_url'] = '/test/page/';
//		$config['total_rows'] = '200';
//		$config['per_page'] = '20';
//		$this->load->driver('cache');
//		$this->cache->file->save('foo', array('bar'), 10);
//		print_r( $this->cache->file->get('foo'));die(0);
//		$this->load->view('front/index', $this->data);
		//$this->pagination->initialize($config);
		
		//echo $this->pagination->create_links();
		//$this->utility->send_mail('laoa_admin@126.com', 'zhanye115@qq.com', '测试的标题', '<a href="http://www.qq.com">测试的内容</a>');
		
		
	}
	
	/**
	 * 举报表单页面
	 */
	public function denounce() {
		if (!$this->authorize->is_logged_in()) {
			echo '-1';die(0);
		}
		if ($_SERVER['REQUEST_METHOD']  == 'POST') {
			
			$from = $this->input->post("from");
			$data['type'] = $this->input->post("type");
			$data['target_type'] = $this->input->post("target_type");
			$data['target_id'] = $this->input->post("target_id");
			$data['content'] = $this->input->post("content");
			$data['created_by'] = $this->authorize->current_user();
			$data['created'] = date("Y-m-d H:i:s");
			$this->load->model("Feedback_model");
			$fid = $this->Feedback_model->add($data);
			if ($from == 'ajax' && $fid > 0) {
				echo '1';die(0);
			}
		}
		$this->load->view('front/denounce', $data);
	}
	
//	public function import() {
//		$this->load->model('Category_model');
//		$content = file_get_contents("d:\\1.txt");
//		$line_array = split("\n", $content);
//		foreach($line_array as $line) {
//			$cate_array = split("\t", $line);
//			
//			if(!empty($cate_array[0])) {
//				$second_level_id = $this->Category_model->insert_child_category($cate_array[0], 8);
//			}
//			
//			for($i = 1, $n = count($cate_array); $i < $n ; $i++) {
//				if (!empty($cate_array[$i]))
//				$this->Category_model->insert_child_category($cate_array[$i], $second_level_id);
//			}
//		}
//	}

//	public function import2() {
//		$this->load->model('Category_model');
//		$first_levels = $this->Category_model->find_son_category(1);
//		$exclude_array = array(228,358);
//		foreach($first_levels as $first_level) {
//			$second_levels = $this->Category_model->find_son_category($first_level['id']);
//			if (!empty($second_levels)) {
//				foreach ($second_levels as $sl) {
//					if (in_array($sl['id'], $exclude_array)) continue;
//					print $sl['name'] . "<br/>";
//					//$this->Category_model->insert_child_category("其它", $sl['id']);
//				}
//			}
//		}
//		$this->Category_model->insert_child_category("其它", 9);
//		$this->Category_model->insert_child_category("其它", 20);
//		
//		
//	}
}